package com.taiji.workOrder.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.taiji.asset.domain.PropertyInfo;
import com.taiji.asset.mapper.PropertyInfoMapper;
import com.taiji.asset.service.IPropertyInfoService;
import com.taiji.common.utils.JwtUtil;
import com.taiji.common.utils.uuid.IdUtils;
import com.taiji.workOrder.domain.ServerMonitorData;
import com.taiji.workOrder.dto.ServerMonitorDataDTO;
import com.taiji.workOrder.dto.ServerVO;
import com.taiji.workOrder.mapper.ServerMonitorDataMapper;
import com.taiji.workOrder.service.IMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author -1
 * @since 2023/08/08 16:35
 * description:
 */
@Service
public class MonitorServiceImpl implements IMonitorService {

    @Autowired
    private ServerMonitorDataMapper serverMonitorDataMapper;

    @Autowired
    private PropertyInfoMapper propertyInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertServerMonitorData(ServerMonitorDataDTO serverMonitorDataDTO) {

        String token = serverMonitorDataDTO.getToken();
        try {
            JwtUtil.verify(token);
        }catch (Exception e) {
            //token有误
            return 0;
        }
        //获取项目代码
        String projectCode = JwtUtil.getTokenValue(token, "projectCode", String.class);
        //如果该项目代码不存在数据则新增数据,存在这删除旧数据,插入新数据
        ServerMonitorData serverMonitorData = new ServerMonitorData();
        serverMonitorData.setProjectCode(projectCode);
        //逻辑删除旧数据
        serverMonitorData.setIsDeleted(1);
        serverMonitorDataMapper.updateServerMonitorDataByProjectCode(serverMonitorData);
        //插入新数据
        Map<String, JSONObject> data = serverMonitorDataDTO.getData();
        for (String serverAddress : data.keySet()) {
            JSONObject jsonObject = data.get(serverAddress);
            ServerMonitorData serverMonitorData1 = new ServerMonitorData();
            serverMonitorData1.setId(IdUtils.randomUUID());
            serverMonitorData1.setProjectCode(projectCode);
            serverMonitorData1.setServerAddress(serverAddress);
            serverMonitorData1.setData(jsonObject.toJSONString());
            serverMonitorData1.setCreateTime(new Date());
            serverMonitorDataMapper.insertServerMonitorData(serverMonitorData1);
        }
        return 1;
    }

    @Override
    public ServerMonitorData getMonitorDataByAddress(ServerMonitorData serverMonitorData) {
        String serverAddress = serverMonitorData.getServerAddress();
        return  serverMonitorDataMapper.selectServerMonitorDataByServerAddress(serverAddress);
    }

    @Override
    public List<ServerVO> getServerMonitorDataList(ServerMonitorData query) {
        List<ServerVO> result = new ArrayList<>();

        PropertyInfo propertyInfo1 = new PropertyInfo();
        propertyInfo1.setProjectCode(query.getProjectCode());
        List<PropertyInfo> propertyInfos = propertyInfoMapper.selectPropertyInfoServerAddressList(propertyInfo1);
        for (PropertyInfo propertyInfo : propertyInfos) {
            ServerMonitorData serverMonitorData = serverMonitorDataMapper.selectServerMonitorDataByServerAddress(propertyInfo.getServerAddress());
            ServerVO serverVO = new ServerVO();
            serverVO.setServerName(propertyInfo.getServerName());
            serverVO.setServerAddress(propertyInfo.getServerAddress());
            if (serverMonitorData!=null){
                String data = serverMonitorData.getData();
                JSONObject jsonObject = JSONObject.parseObject(data);
                List<JSONObject> list = jsonObject.getJSONObject("cpu").getList("cpu_core_number_query", JSONObject.class);
                if (!list.isEmpty()){
                    String cpu = list.get(0).getList("value", String.class).get(1);
                    serverVO.setTotalNuclearNumber(cpu);
                }
                List<JSONObject> list1 = jsonObject.getJSONObject("mem").getList("mem_total_query", JSONObject.class);
                if (!list1.isEmpty()) {
                    String storage = list1.get(0).getList("value", String.class).get(1);
                    serverVO.setStorage(storage);
                }
                List<JSONObject> list2 = jsonObject.getJSONObject("load").getList("load1_query", JSONObject.class);
                if (!list2.isEmpty()){
                    String load = list2
                            .get(0).getList("value", String.class).get(1);
                    serverVO.setSystemLoad(load);
                }
                List<JSONObject> list3 = jsonObject.getJSONObject("mem").getList("mem_use_percent_query", JSONObject.class);
                if (!list3.isEmpty()) {
                    String free = list3.get(0).getList("value", String.class).get(1);
                    serverVO.setMemoryUsage(free);
                }
                //todo 根分区使用率
                List<JSONObject> list4 = jsonObject.getJSONObject("disk").getList("disk_use_percent_query", JSONObject.class);
                if (!list4.isEmpty()) {
                    String rootPartitionUsage = list4.get(0).getList("value", String.class).get(1);
                    serverVO.setRootPartitionUsage(rootPartitionUsage);
                }
            }
            result.add(serverVO);
        }

        return result;
    }
}
